/**
 * Created by Administrator on 2018/5/10.
 */

$(function() { // 工厂函数start

    var doctorId = getQueryString("doctorId");
    $("#noticeContent_link").attr("href","../studio/notice_add.html?doctorId="+doctorId);
    $("#introduce_link").attr("href","../studio/notice_doctor_info.html?doctorId="+doctorId);
    $("#specialty_link").attr("href","../studio/notice_good_at.html?doctorId="+doctorId);
    $("#noticeDescription_link").attr("href","../studio/notice_welcome.html?doctorId="+doctorId);


    // 加载医生信息
    $.ajax({
        url: "/d/doctor/getDoctorInfo",
        type: "post",
        dataType: "json",
        data: {"doctorId":doctorId},
        success: function (result) {
            var doctorInfo = "";
            doctorInfo += "<h3 class='weui-media-box__title' style='font-size: 16px'>" + result.data.doctorname + "</h3>" +
                "<p class='weui-media-box__desc' style='font-size: 11px;'>" + result.data.sex + " / 18岁</p>";
            if(result.data.medicalInstitution!=null){
                doctorInfo +="<p class='weui-media-box__desc' style='font-size: 11px;'>" + result.data.medicalInstitution.medicalname + "</p>" +
                "<span class='link-array link-array-right'></span>";
            }else{
                doctorInfo+="<p class=\"weui-media-box__desc\" style=\"font-size: 11px;\">该医生暂未设置所属医疗机构</p>"+
                "<span style='float: right'></span><span class='link-array link-array-right'></span>";
            }
            $("#doctorInfo").html(doctorInfo);

            // 验证是否已创建公告信息
            addNotice(result.data.doctorId);
        }, error: function () {
            alert("ajax出错");
        }
    });

    // 更新公告内容
    $(".addContent").click(function () {
        //alert(doctorId);
        // 定义请求参数，医生编号与公告内容
        var data = {"doctorId": doctorId, "noticeContent": $("#noticeContent").val()};
        $.ajax({
            url: "/notice/updateNotice",
            type: "post",
            dataType: "json",
            data: data,
            success: function (result) {
                console.log(result.msg);
                window.location.href='../studio/notice_setting.html?doctorId='+doctorId;
                //alert(result.msg)
            }, error: function () {
                //alert("ajax出错");
            }
        });
    });// 更新公告内容ent

    // 更新简介信息
    $(".addIntroduce").click(function () {
        //alert(doctorId);
        // 定义请求参数，医生编号与公告内容
        var data = {"doctorId": doctorId, "introduce": $("#introduce").val()};
        $.ajax({
            url: "/notice/updateNotice",
            type: "post",
            dataType: "json",
            data: data,
            success: function (result) {
                console.log(result.msg);
                window.location.href='../studio/notice_setting.html?doctorId='+doctorId;
                //alert(result.msg)
            }, error: function () {
                //alert("ajax出错");
            }
        });
    });     // 更新简介end

    // 更新擅长信息
    $(".addSpecialty").click(function () {
        //alert(doctorId);
        // 定义请求参数，医生编号与公告内容
        var data = {"doctorId": doctorId, "specialty": $("#specialty").val()};
        $.ajax({
            url: "/notice/updateNotice",
            type: "post",
            dataType: "json",
            data: data,
            success: function (result) {
                console.log(result.msg);
                window.location.href='../studio/notice_setting.html?doctorId='+doctorId;
                //alert(result.msg)
            }, error: function () {
                //alert("ajax出错");
            }
        });
    });     // 更新擅长end

    // 更新描述信息，欢迎语
    $(".addDescription").click(function () {
        //alert(doctorId);
        // 定义请求参数，医生编号与公告内容
        var data = {"doctorId": doctorId, "description": $("#noticeDescription").val()};
        $.ajax({
            url: "/notice/updateNotice",
            type: "post",
            dataType: "json",
            data: data,
            success: function (result) {
                console.log(result.msg);
                window.location.href='../studio/notice_setting.html?doctorId='+doctorId;
                //alert(result.msg)
            }, error: function () {
                //alert("ajax出错");
            }
        });
    });     // 更新描述欢迎语end
});// 工厂函数end
    function addNotice(doctorId){
        $.ajax({
            url:"/notice/insertNotice",
            type:"post",
            dataType:"json",
            data:{"doctorId":doctorId},
            success:function (result) {
                console.log(result.msg);
                if(result.msg=='已创建过') {
                    if(result.data.chargeFee == 0) {
                        $("#chargeFee").html("免费");
                        $("#chargeFee_link").attr("href","../studio/notice_charge_set.html?doctorId="+doctorId+"&chargeFee=0");
                    }else{
                        $("#chargeFee").html("￥"+result.data.chargeFee);
                        $("#chargeFee_link").attr("href","../studio/notice_charge_set.html?doctorId="+doctorId+"&chargeFee="+result.data.chargeFee);
                    }
                    $("#chargeDesc").html(result.data.chargeDesc);
                    $("#noticeContent").html(result.data.notice);
                    $("#introduce").html(result.data.introduce);
                    $("#specialty").html(result.data.goodAt);
                    $("#noticeDescription").html(result.data.welcomeWords);
                }
            },error:function () {
                //alert("ajax出错");
            }
        });
    }